<?php
namespace Client\Controller;
use Think\Controller;
class BaseController extends Controller {
	
	public static $wx;//缓存微信对象
	public static $entry;
	protected function _initialize(){

		if(!strpos($_SERVER["HTTP_USER_AGENT"],"MicroMessenger")){
			//$this->diemsg(0,'请使用微信浏览器访问本应用！');
		}
		
		self::$entry=array('1'=>'普洗袋洗','2'=>'品牌精洗','3'=>'洗鞋','4'=>'洗涤救治','5'=>'家政服务','6'=>'奢侈品护理');
		
		$options['appid']=C('BB_APPID_client');
		$options['secret']=C('BB_SECRET_client');
		self::$wx=new \bb\Wechat($options);
		
		//unset($_SESSION);
		//微信授权===================
		if(!$_SESSION['openid']){
			if(I('code')){
				$d=self::$wx->getOauthAccessToken();
				$u=self::$wx->user($d['openid']);
				if($u['subscribe']){
					$_SESSION['openid']=$u['openid'];
					$_SESSION['wxuser']=$u;
					$_SESSION['set']['url']=C('BB_URL');
				}else{
					//没有关注公众号跳转到关注页面
					//$url="http://mp.weixin.qq.com/s?__biz=MzAxOTE2Mjk3OQ==&mid=208409118&idx=1&sn=c4c13c5e576a0bb1dd1f940459009279#rd";
					header("Location:".$url);
					exit;
				}
			}else{
				$url=$this->snsapi_base();
				header("location:".$url);
			}
		}
		//=end===========================
		//用户数据=========================
		$openid=$_SESSION['openid'];
		$client_user=M('client_user')->where(array('openid'=>$openid))->find();
		if(!$client_user){
			if($openid){
				$c_u['openid']=$openid;
				$c_u['username']=$_SESSION['wxuser']['nickname'];
				if($_SESSION['wxuser']['sex']==1){$sex='男';}elseif($_SESSION['wxuser']['sex']==2){$sex='女';}else{$sex=' '; }
				$c_u['sex']=$sex;
				$c_u['integral']='100';  //首次注册100积分 
				M('client_user')->add($c_u);
				
				$log['openid']=$openid;
				$log['addtime']=time();   
				$log['type']='1';   //积分日志
				$log['msg']='积分增加：首次注册送100个积分';
				M('log_msg')->add($log);
			}
		}
		//=================
		//时时更新用户的位置信息
		$udata=M('weixin_client_user')->field('latitude,longitude')->where(array('openid'=>$openid))->find();
		if($udata['latitude'] && $udata['longitude']){
			$wz=$this->get_position_pc($udata);
			$_SESSION['wz']=$wz;
			//如果地区 或者城市 为空的的话就 按用户说在的位置来更新
			if(!$client_user['area'] || !$client_user['city']){
				M('client_user')->where(array('openid'=>$openid))->save(array('area'=>$wz['city'].$wz['district'],'city'=>$wz['city']));
			}
			if(!$_SESSION['city']){
				$city=M('bb_area')->where(array('title'=>$client_user['city']))->find();
				$_SESSION['city']=$city;
			}
		}
		$_SESSION['user']=$client_user;
		//=end===========================
	}
	//静默授权URl
	public function snsapi_base(){
		$oa_url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		return self::$wx->getOAuthRedirect($oa_url,'xybingbing');
	}
	//停止不动的信息通知页面处理
	public function diemsg($status,$msg){
		//成功为1，失败为0
		$status=$status?$status:'0';
		$this->assign('status',$status);
		$this->assign('msg',$msg);
		$this->display('Base_diemsg');
		die();
	}
	//百度根据坐标找位置
	private function get_position_pc($zb=''){
		if($zb){
			$url = 'http://api.map.baidu.com/geocoder?location='.$zb['latitude'].','.$zb['longitude'].'&coord_type=bd09ll&output=json&src=jt';
			$addressData = get_curl($url);
			$address = json_decode($addressData,true);
			if($address['status']=='OK'){
				$addres['address']=$address['result']['formatted_address'];
				$addres['longitude']=$address['result']['location']['lng'];
				$addres['latitude']=$address['result']['location']['lat'];
				$addres['province']=$address['result']['addressComponent']['province'];		//省
				$addres['city']=$address['result']['addressComponent']['city'];						//市
				$addres['district']=$address['result']['addressComponent']['district'];			//区
				return $addres;
			}else{
				return FALSE;
			}
		}
	}
	
//	//获取单张图片
//	public function getPic($id){
//		$m=M('weixin_upload_img');
//		$map['id']=$id;
//		$list=$m->where($map)->find();
//		if($list){
//			$list['imgurl']="/Upload/".$list['savepath'].$list['savename'];
//		}
//		return $list?$list:"";
//	}
}